/*
 * Copyright 2011 Soluvas.
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License (GPLv2 only)
 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 */
package com.soluvas.antldap;

import org.apache.tools.ant.BuildException;

import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.Modification;
import com.unboundid.ldap.sdk.ModificationType;

/**
 * @author quentin
 */
public class AttrModTask extends LdapTask {

	private String dn;
	private String attribute;

	public void setAttributeValue(String attributeValue) {
		this.attributeValue = attributeValue;
	}

	private String attributeValue;
	private boolean echo;

	@Override
	public void execute() throws BuildException {
		connectLdapWith(new Runnable() {
			@Override
			public void run() {

				try {
					Modification modification = new Modification(
							ModificationType.REPLACE, attribute, attributeValue);
					connection.modify(dn, modification);

					log("(" + dn + ") " + attribute + " set to"
							+ attributeValue, 4);
					if (echo)
						System.out.println(attribute + " set to "
								+ attributeValue);
				} catch (LDAPException e) {
					throw new BuildException("Cannot find LDAP entry " + dn + " on "+ getUri(), e);
				}

			}
		});
	}

	public String getDn() {
		return dn;
	}

	public void setDn(String dn) {
		this.dn = dn;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public void setEcho(boolean echo) {
		this.echo = echo;
	}

	public boolean isEcho() {
		return echo;
	}

}
